# -*- coding: utf-8 -*-
#
# jQuery File Tree
# Mod-Python Connector Script
# By Suchindra Chandrahas
#
from mod_python import apache
from mod_python import util
import os

def dirlist(req):
   req.content_type='text/data;charset=utf-8'

   handler = req.form

   docRoot = req.document_root()

   folderName = "/" + handler['dir']

   r = ['<ul class="jqueryFileTree" style="display: none;">']
   try:
       r = ['<ul class="jqueryFileTree" style="display: none;">']
       for f in os.listdir(docRoot + folderName):
           ff=os.path.join(folderName,f)
           if os.path.isdir(ff):
               r.append('<li id="folderLi" ><a href="#" onClick="parent.fileClicked(rel)" onmousedown="parent.rightButtonClicked()" rel="%s/">%s</a></li>' % (ff,f))
           else:
               e=os.path.splitext(f)[1][1:] # get .ext and remove dot
               r.append('<li id="fileLi" class="file ext_%s"><a onClick="parent.fileClicked(rel)" onmousedown="parent.rightButtonClicked(rel)" href="#" rel="%s">%s</a></li>' % (e,ff,f))
       r.append('</ul>')
   except Exception,e:
       r.append('Could not load directory: %s' % str(e))
   r.append('</ul>')

   response = "".join(r)

   req.set_content_length(len(response))   
   req.write(response, 0)
   req.flush()
   return apache.OK

